/**
 * Created by SCR on 2017/8/1.
 */
var RmtReqCtrl = require('../remoteReqControl');
var xmlparse = require('xml2js').parseString;

module.exports = (function () {
    var __getMSDetail = function (MS, cb) {
        var url = 'http://'+MS.host+':'+MS.port+'/integration/SADL/getMSDetail';
        var form = {
            _id : MS._id
        };
        RmtReqCtrl.getByServer(url,form,function (err, res) {
            // res:{
            //     MS:Object,
            //     MDLBuffer: Buffer
            // }
            if(err) return cb(err);
            res = JSON.parse(res);
            if(res.error) return cb(res.error);
            var MDLStr = res.MSDetail.MDLStr;
            xmlparse(MDLStr, { explicitArray : false, ignoreAttrs : false }, function (err, MDL) {
                if(err) {
                    console.log('Error in parse mdl file : ' + err);
                    return cb(err);
                }
                var strMDL = JSON.stringify(MDL);
                strMDL = strMDL.replace(/\"\$\"/g,'"_$"');
                MDL = JSON.parse(strMDL);

                res.MSDetail.MDL = MDL;
                res.MSDetail.host = MS.host;
                res.MSDetail.port = MS.port;
                res.MSDetail.MDLStr = MDLStr;
                // res.MSDetail._id = res.MSDetail.MS._id;
                return cb(null,res.MSDetail);
            });
        })
    };

    return {
        getMS: function (host, port, cb) {
            RmtReqCtrl.getByServer('http://' + host + ':' + port + '/modelser/json/all', {}, function(err, msList){
                if(err){
                    return cb(err);
                }
                else{
                    msList = JSON.parse(msList);
                    msList = msList.data;
                    for(var i=0;i<msList.length;i++){
                        msList[i].host = host;
                        msList[i].port = port;
                        msList[i].value = msList[i].ms_model.m_name;
                    }
                    return cb(null,msList);
                }
            });
        },

        getDS: function (host, port, cb) {
            var count = 0;
            var dataServices = {error:null};
            var refactorServices = {error:null};
            var dataMapServices = {error:null};

            var dataServiceURL = 'http://'+host+':'+port+'/common/services?pageamount=10&page=0&username=admin&ids=all&type=data';
            var dataMapServiceURL = 'http://'+host+':'+port+'/common/services?pageamount=10&page=0&username=admin&ids=all&type=datamap';
            var refactorServiceURL = 'http://'+host+':'+port+'/common/services?pageamount=10&page=0&username=admin&ids=all&type=refactor';

            function resData() {
                if(count == 1){
                    return cb(null,{
                        dataServices: dataServices,
                        dataMapServices: dataMapServices,
                        refactorServices: refactorServices
                    });
                }
            }

            // RmtReqCtrl.getByServer(dataServiceURL, null, function(err, serviceList){
            //     count++;
            //     if(err){
            //         dataServices.error = err;
            //     }
            //     else{
            //         serviceList = JSON.parse(serviceList);
            //         dataServices.data = serviceList;
            //     }
            //     resData();
            // });

            RmtReqCtrl.getByServer(dataMapServiceURL, null, function(err, serviceList){
                count++;
                if(err){
                    dataMapServices.error = err;
                }
                else{
                    serviceList = JSON.parse(serviceList);
                    dataMapServices.data = serviceList;
                }
                resData();
            });

            // RmtReqCtrl.getByServer(refactorServiceURL, null, function(err, serviceList){
            //     count++;
            //     if(err){
            //         refactorServices.error = err;
            //     }
            //     else{
            //         serviceList = JSON.parse(serviceList);
            //         refactorServices.data = serviceList;
            //     }
            //     resData();
            // });
        },

        getMSDetailList: function (msList, cb) {
            var MSDetailList = [];
            var count = 0;
            var pending = function () {
                count++;
                return function (err, MSDetail) {
                    count--;
                    if(err){
                        MSDetailList.push({error:err});
                    }
                    else{
                        MSDetailList.push({error:null,MSDetail:MSDetail});
                    }
                    if(count == 0){
                        return cb(null,MSDetailList);
                    }
                }
            };
            for(var i=0;i<msList.length;i++){
                __getMSDetail(msList[i],pending());
            }
        }
    }
})();